<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Python for 循环语句 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Python for 循环语句</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Python/">Python</a>
			</span>
		
	</div>

	

	
		<h1 id="Python-for-循环语句"><a href="#Python-for-循环语句" class="headerlink" title="Python for 循环语句"></a>Python for 循环语句</h1><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python for循环可以遍历任何序列的项目，如一个列表或者一个字符串。</p>
<h2 id="语法："><a href="#语法：" class="headerlink" title="语法："></a>语法：</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;for循环的语法格式如下：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">for</span> iterating_var <span class="keyword">in</span> sequence:</div><div class="line">   statements(s)</div></pre></td></tr></table></figure>
<h2 id="流程图："><a href="#流程图：" class="headerlink" title="流程图："></a>流程图：</h2><p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/python%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6-for%E5%BA%8F%E5%88%97/01.jpg?raw=true" alt="images"><figcaption class="figure__caption">images</figcaption></figure></p>
<h3 id="实例"><a href="#实例" class="headerlink" title="实例"></a>实例</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></div><div class="line"> </div><div class="line"><span class="keyword">for</span> letter <span class="keyword">in</span> <span class="string">'Python'</span>:     <span class="comment"># 第一个实例</span></div><div class="line">   <span class="keyword">print</span> <span class="string">'当前字母 :'</span>, letter</div><div class="line"> </div><div class="line">fruits = [<span class="string">'banana'</span>, <span class="string">'apple'</span>,  <span class="string">'mango'</span>]</div><div class="line"><span class="keyword">for</span> fruit <span class="keyword">in</span> fruits:        <span class="comment"># 第二个实例</span></div><div class="line">   <span class="keyword">print</span> <span class="string">'当前水果 :'</span>, fruit</div><div class="line"> </div><div class="line"><span class="keyword">print</span> <span class="string">"Good bye!"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以上实例输出结果:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">当前字母 : P</div><div class="line">当前字母 : y</div><div class="line">当前字母 : t</div><div class="line">当前字母 : h</div><div class="line">当前字母 : o</div><div class="line">当前字母 : n</div><div class="line">当前水果 : banana</div><div class="line">当前水果 : apple</div><div class="line">当前水果 : mango</div><div class="line">Good bye!</div></pre></td></tr></table></figure>
<h2 id="通过序列索引迭代"><a href="#通过序列索引迭代" class="headerlink" title="通过序列索引迭代"></a>通过序列索引迭代</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;另外一种执行循环的遍历方式是通过索引，如下实例：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></div><div class="line"> </div><div class="line">fruits = [<span class="string">'banana'</span>, <span class="string">'apple'</span>,  <span class="string">'mango'</span>]</div><div class="line"><span class="keyword">for</span> index <span class="keyword">in</span> range(len(fruits)):</div><div class="line">   <span class="keyword">print</span> <span class="string">'当前水果 :'</span>, fruits[index]</div><div class="line"> </div><div class="line"><span class="keyword">print</span> <span class="string">"Good bye!"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以上实例输出结果：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">当前水果 : banana</div><div class="line">当前水果 : apple</div><div class="line">当前水果 : mango</div><div class="line">Good bye!</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以上实例我们使用了内置函数 len() 和 range(),函数 len() 返回列表的长度，即元素的个数。 range返回一个序列的数。</p>
<h2 id="循环使用-else-语句"><a href="#循环使用-else-语句" class="headerlink" title="循环使用 else 语句"></a>循环使用 else 语句</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在 python 中，for … else 表示这样的意思，for 中的语句和普通的没有区别，else 中的语句会在循环正常执行完（即 for 不是通过 break 跳出而中断的）的情况下执行，while … else 也是一样。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></div><div class="line"> </div><div class="line"><span class="keyword">for</span> num <span class="keyword">in</span> range(<span class="number">10</span>,<span class="number">20</span>):  <span class="comment"># 迭代 10 到 20 之间的数字</span></div><div class="line">   <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">2</span>,num): <span class="comment"># 根据因子迭代</span></div><div class="line">      <span class="keyword">if</span> num%i == <span class="number">0</span>:      <span class="comment"># 确定第一个因子</span></div><div class="line">         j=num/i          <span class="comment"># 计算第二个因子</span></div><div class="line">         <span class="keyword">print</span> <span class="string">'%d 等于 %d * %d'</span> % (num,i,j)</div><div class="line">         <span class="keyword">break</span>            <span class="comment"># 跳出当前循环</span></div><div class="line">   <span class="keyword">else</span>:                  <span class="comment"># 循环的 else 部分</span></div><div class="line">      <span class="keyword">print</span> num, <span class="string">'是一个质数'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以上实例输出结果：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="number">10</span> 等于 <span class="number">2</span> * <span class="number">5</span></div><div class="line"><span class="number">11</span> 是一个质数</div><div class="line"><span class="number">12</span> 等于 <span class="number">2</span> * <span class="number">6</span></div><div class="line"><span class="number">13</span> 是一个质数</div><div class="line"><span class="number">14</span> 等于 <span class="number">2</span> * <span class="number">7</span></div><div class="line"><span class="number">15</span> 等于 <span class="number">3</span> * <span class="number">5</span></div><div class="line"><span class="number">16</span> 等于 <span class="number">2</span> * <span class="number">8</span></div><div class="line"><span class="number">17</span> 是一个质数</div><div class="line"><span class="number">18</span> 等于 <span class="number">2</span> * <span class="number">9</span></div><div class="line"><span class="number">19</span> 是一个质数</div></pre></td></tr></table></figure>
	

	
		<span class="different-posts"><a href="/2017/10/12/Python/1. Python 基础篇/1. Python 基础语法/2.2 流程控制-for 序列/13. Python for 循环语句/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
